package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.constant.LimitCon;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.SysNoticeService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysVipRules;
import com.ruoyi.system.service.ISysVipRulesService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 会员规则Controller
 * 
 * @author ruoyi
 * @date 2024-03-11
 */
@RestController
@RequestMapping("/system/rules")
public class SysVipRulesController extends BaseController
{
    @Autowired
    private ISysVipRulesService sysVipRulesService;

    /**
     * 查询会员规则列表
     */
    @PreAuthorize("@ss.hasPermi('system:rules:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysVipRules sysVipRules)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        if (user.isAdmin())
        {
            sysVipRules.setOwnerid("1");
        }else{
            sysVipRules.setOwnerid(user.getUserId()+"");
        }
        startPage();
        List<SysVipRules> list = sysVipRulesService.selectSysVipRulesList(sysVipRules);
        return getDataTable(list);
    }

    /**
     * 导出会员规则列表
     */
    @PreAuthorize("@ss.hasPermi('system:rules:export')")
    @Log(title = "会员规则", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysVipRules sysVipRules)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        if (user.isAdmin())
        {
            sysVipRules.setOwnerid("1");
        }else{
            sysVipRules.setOwnerid(user.getUserId()+"");
        }
        List<SysVipRules> list = sysVipRulesService.selectSysVipRulesList(sysVipRules);
        ExcelUtil<SysVipRules> util = new ExcelUtil<SysVipRules>(SysVipRules.class);
        util.exportExcel(response, list, "会员规则数据");
    }

    /**
     * 获取会员规则详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:rules:query')")
    @GetMapping(value = "/{ruleId}")
    public AjaxResult getInfo(@PathVariable("ruleId") Long ruleId)
    {
        return success(sysVipRulesService.selectSysVipRulesByRuleId(ruleId));
    }

    /**
     * 新增会员规则
     */
    @PreAuthorize("@ss.hasPermi('system:rules:add')")
    @Log(title = "会员规则", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysVipRules sysVipRules)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        SysVipRules querySysVipRules=new SysVipRules();
        if (user.isAdmin())
        {
            sysVipRules.setOwnerid("1");
            querySysVipRules.setOwnerid("1");
        }else{
            sysVipRules.setOwnerid(user.getUserId()+"");
            querySysVipRules.setOwnerid(user.getUserId()+"");
        }
        //如果新增的是 启用状态，需要查看目前启用状态的有几条
        if (sysVipRules.getStatus() == 0) {
            querySysVipRules.setStatus(0);
            List result = sysVipRulesService.selectSysVipRulesList(querySysVipRules);
            if (result != null && result.size() > 0) {
                return AjaxResult.error("已有启用状态的条目，只允许一条启用状态数据");
            }
        }
        List result =  sysVipRulesService.selectSysVipRulesList(querySysVipRules);
        if (result != null && result.size() > LimitCon.MaxRowSize-1) {
            return AjaxResult.error("已超出最多条目数，只允许存在" + LimitCon.MaxRowSize +"条数据");
        }
        return toAjax(sysVipRulesService.insertSysVipRules(sysVipRules));
    }

    /**
     * 修改会员规则
     */
    @PreAuthorize("@ss.hasPermi('system:rules:edit')")
    @Log(title = "会员规则", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysVipRules sysVipRules)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        SysVipRules querySysVipRules=new SysVipRules();
        if (user.isAdmin())
        {
             querySysVipRules.setOwnerid("1");
        }else{
             querySysVipRules.setOwnerid(user.getUserId()+"");
        }
        //如果新增的是 启用状态，需要查看目前启用状态的有几条
        if (sysVipRules.getStatus() == 0) {
            querySysVipRules.setStatus(0);
            List<SysVipRules> result = sysVipRulesService.selectSysVipRulesList(querySysVipRules);
            if (result != null && result.size() > 0) {
                boolean isself=false;
                for (int i=0;i<result.size();i++){
                    SysVipRules ee=result.get(i);
                    if (ee.getRuleId().equals(sysVipRules.getRuleId())){
                        isself=true;
                        break;
                    }
                }
                if(!isself){
                    return AjaxResult.error("已有启用状态的条目，只允许一条启用状态数据");
                }
            }
        }
        return toAjax(sysVipRulesService.updateSysVipRules(sysVipRules));
    }

    /**
     * 删除会员规则
     */
    @PreAuthorize("@ss.hasPermi('system:rules:remove')")
    @Log(title = "会员规则", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ruleIds}")
    public AjaxResult remove(@PathVariable Long[] ruleIds)
    {
        return toAjax(sysVipRulesService.deleteSysVipRulesByRuleIds(ruleIds));
    }
}
